﻿<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

Class Mother extends MY_Model {

    public function __construct() {
        parent::__construct();
    }

    public function saveEmailDB($mac, $nasid, $id_campaign, $name_device, $id_place, $email) {
        if ($this->valid_email($email) == 1) {
            return '202'; //Lỗi email
        }
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'email' => $email
        );
        $this->m_db->insert('log_email', $arr);
        return '200';
    }

    public function saveMobileDB($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile) {
        if ($this->min_length($mobile, 10) == 1) {
            return '202';
        }
        if ($this->min_length($mobile, 10) == 2) {
            return '202';
        }

        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'mobile' => $mobile
        );
        $this->m_db->insert('log_mobile', $arr);
        return '200';
    }

    public function saveFacebookDB($mac) {
        $fbid = $this->getParamString('fbid');
        $email = $this->getParamString('email');
        $fullname = $this->getParamString('fullname');
        if ($this->valid_email($email) == 1) {//die('sad');
            return 202;
        }
        $id_shop = $this->db->query('SELECT id FROM shop WHERE MaC = "' . $mac . '"')->row_object()->id;
        $check = $this->m_db->where(array('id_shop' => intval($id_shop), 'fbid' => $fbid))->count('log_facebook');
        if ($check == 0 && $id_shop > 0) {
            ini_set('mongo.native_long', 1);
            $now = date("Y-m-d H:i:s", time());
            $nr = date('YmdHis');
            $cd__ = new MongoInt64($nr);
            $arr = array(
                'fbid' => $fbid,
                'email' => $email,
                'fullname' => $fullname,
                'id_shop' => intval($id_shop),
                'time_access' => date('Y-m-d H:i:s'),
                'show_time' => date('d-m-Y'),
                'show_full_time' => date('H:i:s d-m-Y'),
                'time_vs' => $cd__,
                'MAC' => $mac
            );
            $this->m_db->insert('log_facebook', $arr);
            return '200';
        } else {
            return '201';
        }
    }

    public function saveFacebookshareDB($mac, $nasid) {
        $facebook_url = $this->getParamString('facebook_url');
        $id_campaign = $this->getParamString('id_campaign');
        $fbid = $this->getParamString('fbid');
        $email = $this->getParamString('email');
        $fullname = $this->getParamString('fullname');
        $name_device = $this->getParamString('name_device');
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'fbid' => $fbid,
            'email' => $email,
            'fullname' => $fullname,
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'facebook_url' => $facebook_url
        );
        $this->m_db->insert('log_facebook', $arr);
        return '200';
    }

    public function saveFacebookLikeDB($fbid, $called) {
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'fbid' => strtolower($fbid),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'MAC' => $called
        );
        $this->m_db->insert('log_likefb', $arr);
        return '200';
    }

    /* Truy cập miễn phí */

    public function saveFreeDb($mac, $nasid, $id_campaign, $name_device) {
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device
        );
        $this->m_db->insert('log_free', $arr);
        return '200';
    }

    /* Truy cập Default */

    public function savecampaignDefaultDB($mac, $nasid, $id_campaign, $name_device) {
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device
        );
        $this->m_db->insert('log_default', $arr);
        return '200';
    }

    /* Truy cập website */

    public function saveVisitDb($mac, $nasid, $id_campaign, $name_device, $link) {
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'website_url' => $link
        );
        $this->m_db->insert('log_accessweb', $arr);
        return '200';
    }

    public function saveVideoDb($mac, $nasid, $id_campaign, $name_device, $video_link) {
        $id_place = $this->db->query('SELECT id_place FROM modem WHERE nas_id = "' . $nasid . '"')->row_object()->id_place;
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'video_link' => $video_link
        );
        $this->m_db->insert('log_video', $arr);
        return '200';
    }

    public function saveSurveyDb($mac, $nasid, $id_campaign, $name_device, $id_place, $formvalue) {
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'formvalue' => $formvalue
        );
        $this->m_db->insert('log_survey', $arr);
        return '200';
    }

    public function saveWifiPasswordDb($mac, $nasid, $id_campaign, $name_device, $id_place, $password) {
        ini_set('mongo.native_long', 1);
        $now = date("Y-m-d H:i:s", time());
        $nr = date('YmdHis');
        $cd__ = new MongoInt64($nr);
        $arr = array(
            'id_place' => intval($id_place),
            'time_access' => date('Y-m-d H:i:s'),
            'show_time' => date('d-m-Y'),
            'show_full_time' => date('H:i:s d-m-Y'),
            'time_vs' => $cd__,
            'mac' => $mac,
            'nasid' => $nasid,
            'id_campaign' => (int) $id_campaign,
            'name_device' => $name_device,
            'password' => $password
        );
        $this->m_db->insert('log_wifipassword', $arr);
        return '200';
    }

    public function campaignCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile_email, $type) {

        if ($type == 1) {
            if ($this->min_length($mobile_email, 10) == 1) {
                return '202';
            }
            if ($this->min_length($mobile_email, 10) == 2) {
                return '202';
            }
        } else {
            if ($this->valid_email($mobile_email) == 1) {
                return '202'; //Lỗi email
            }
        }

        $time_active = $this->db->query('SELECT time_active FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object()->time_active;
        if (in_array($time_active, array('', null))) {
            return '204'; // ko có time active
        }
        $now_ = new MongoInt64(date("YmdHis", time()));
        $query = $this->m_db->where(array('id_campaign' => (int) $id_campaign, 'active' => 0))->where_gt('time_end_vs', $now_)->limit(1);
        $result = $query->get('log_campaign_coupon');
        if (count($result) == 0) {
            return '201'; // hết rồi ko có coupon để nhận đâu cưng ơi
        } else {

            $oneview = $this->db->query('SELECT oneview FROM campaign WHERE id = ' . $id_campaign . ' LIMIT 1')->row_object()->oneview;
            if (in_array($oneview, array('', null))) {
                $oneview = 1;
            }
            // count interact
            if ($type == 1) {
                $count_interact = $this->m_db->where(array('mobile' => (string) $mobile_email, 'id_campaign' => (int) $id_campaign))->count('log_campaign_coupon');
                if ($count_interact >= $oneview) {
                    return '205';
                }
            } else {
                if ($type == 2) {
                    $count_interact = $this->m_db->where(array('email' => (string) $mobile_email, 'id_campaign' => (int) $id_campaign))->count('log_campaign_coupon');
                    if ($count_interact >= $oneview) {
                        return '205';
                    }
                }
            }
            //return 'count_interact:'.$count_interact;

            $data = $result;
            //die(''.$data[0]['code_coupon']);
            //echo '<pre>';die(print_r($data));
            ini_set('mongo.native_long', 1);
            $now = date("Y-m-d H:i:s", time());
            $nr = date('YmdHis');
            $cd__ = ($nr);
            $cd2__ = (date("YmdHis", time() + 3600 * $time_active));
            $time_start_use = date("Y-m-d H:i:s", time() + 3600 * $time_active);
            $arr = array(
                'type' => (int) $type,
                'name_device' => $name_device,
                'status_sms' => (int) 0,
                'id_place' => intval($id_place),
                'mac' => $mac,
                'nasid' => $nasid,
                'active' => (int) 1, // update đã nhận
                'time_get' => date('H:i:s d-m-Y'),
                'time_get_vs' => $cd__,
                'time_start_use' => $time_start_use,
                'time_start_use_vs' => $cd2__
            );
            if ($type == 1) {
                $arr['mobile'] = $mobile_email;
            } else {
                $arr['email'] = $mobile_email;
                $this->sendEmail($mobile_email, $data[0]['code_coupon'], $time_start_use, $id_place);
            }
            $this->m_db->set($arr)->where(array('id_campaign' => (int) $id_campaign, 'code_coupon' => (string) $data[0]['code_coupon']))->update('log_campaign_coupon', array(), array());
            // VOID() + amount_get 
            $data_amount = $this->db->query('SELECT amount, amount_get FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object();
            if ($data_amount->amount <= $data_amount->amount_get) {
                return '203'; // coupon phát ra <= coupon đã nhận     
            } else {
                $amount_get = (int) $data_amount->amount_get + 1;
                $this->db->update('campaign_coupon', array('amount_get' => $amount_get), 'id_campaign = ' . $id_campaign);
				// sync get coupon wicity to wicard
				$run = file_get_contents('http://125.212.192.114/wicard/api/sync-get-coupon?code_coupon='.(string)$data[0]['code_coupon']);

                return '200';
            }

            /*
              http://127.0.0.1/wicity/index/campaign-coupon?mac=abc&nasid=def&id_place=17&id_campaign=114&name_device=iPhone6&type=2&mobile_email=dungdcth@gmail.com
             */
        }
    }

    public function campaignGiftCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place) {
        $time_active = $this->db->query('SELECT time_active FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object()->time_active;
        if (in_array($time_active, array('', null))) {
            return array('status' => '203', 'content' => 'Không tồn tại time active');
        }
        $now_ = new MongoInt64(date("YmdHis", time()));
        $query = $this->m_db->where(array('id_campaign' => (int) $id_campaign, 'active' => 0))->where_gt('time_end_vs', $now_)->limit(1);
        $result = $query->get('log_campaign_coupon');
        if (count($result) == 0) {
            return array('status' => '204', 'content' => 'Coupon đã hết'); // hết rồi ko có coupon để nhận đâu cưng ơi
        } else {
            $oneview = $this->db->query('SELECT oneview FROM campaign WHERE id = ' . $id_campaign . ' LIMIT 1')->row_object()->oneview;
            if (in_array($oneview, array('', null))) {
                $oneview = 1;
            }

            $data = $result;
            //die(''.$data[0]['code_coupon']);
            //echo '<pre>';die(print_r($data));
            ini_set('mongo.native_long', 1);
            $now = date("Y-m-d H:i:s", time());
            $nr = date('YmdHis');
            $cd__ = ($nr);
            $cd2__ = (date("YmdHis", time() + 3600 * $time_active));
            $time_start_use = date("Y-m-d H:i:s", time() + 3600 * $time_active);
            $arr = array(
                'name_device' => $name_device,
                'status_sms' => (int) 0,
                'id_place' => intval($id_place),
                'mac' => $mac,
                'nasid' => $nasid,
                'active' => (int) 1, // update đã nhận
                'time_get' => date('H:i:s d-m-Y'),
                'time_get_vs' => $cd__,
                'time_start_use' => $time_start_use,
                'time_start_use_vs' => $cd2__
            );
            $this->m_db->set($arr)->where(array('id_campaign' => (int) $id_campaign, 'code_coupon' => (string) $data[0]['code_coupon']))->update('log_campaign_coupon', array(), array());
            // VOID() + amount_get 
            $data_amount = $this->db->query('SELECT amount, amount_get FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object();
            if ($data_amount->amount <= $data_amount->amount_get) {
                return array('status' => '207', 'content' => 'Coupon phát ra nhỏ hơn coupon đã nhận'); // coupon phát ra <= coupon đã nhận     
            } else {
                $amount_get = (int) $data_amount->amount_get + 1;
                $this->db->update('campaign_coupon', array('amount_get' => $amount_get), 'id_campaign = ' . $id_campaign);
                return array('status' => '200', 'content' => 'Bạn đã nhận coupon thành công', 'data' => $data[0]['code_coupon']);
            }
        }
    }

    private function sendEmail($email, $code_coupon, $time_active, $id_place) {
        $config = array(
            'protocol' => 'smtp',
            'smtp_host' => 'mail.gvtechcom.vn',
            'smtp_user' => 'info@gvtechcom.vn',
            'smtp_pass' => 'abcde@123',
            'mailtype' => 'html'
        );
        $time_actives = date('H:i d/m/Y', strtotime($time_active));
        $data_place = $this->db->query('SELECT * FROM place WHERE id = ' . $id_place)->row_object();
        $this->load->library('email', $config);
        $body = "<b>Xin chào " . $email . "</b> <br/> Bạn nhận được mã coupon : <b>" . $code_coupon . "</b><br/>Coupon sử dụng tại : " . $data_place->name . "<br/>Địa chỉ : " . $data_place->address . " <br/>Coupon khuyến mại này có giá trị sử dụng bắt đầu từ " . $time_actives . "<br/>Xin chân trọng cảm ơn! ";
		
        $this->email->from('info@gvtechcom.vn', 'Wicity Việt Nam');
        $this->email->to($email);
        $this->email->subject('Thông báo mã coupon khuyến mại Wicity Việt Nam');
        $this->email->message($body);

/* Config KingBBQ */
		if(in_array($id_campaign, array(273))){
			$this->email->subject('Thông báo mã coupon khuyến mại King BBQ');
			$body = "<b>Xin chào " . $email . "</b> <br/> Bạn nhận được mã coupon : <b>K-DELI5-LN-".$code_coupon."</b> nhận khuyến mãi giảm giá 40% tại nhà hàng buffet lẩu nướng King BBQ-Tầng 4, Số 7 Đinh Tiên Hoàng, Hoàn Kiếm, Hà Nội.<br/>
Tel: 04 3935 0999.<br/><br/>Quý khách vui lòng đặt bàn tại 18001277 (miễn phí cước gọi) hoặc qua đường link dưới đây và mang mã code tới nhà hàng để được nhận ưu đãi<br/>http://kingbbq.com.vn/chuong-trinh-khuyen-mai/nuong-tung-bung-lau-no-ne/?refer=wicity
<br/>Địa chỉ : King BBQ-Tầng 4, Số 7 Đinh Tiên Hoàng, Hoàn Kiếm, Hà Nội.<br/>Coupon khuyến mại này có giá trị sử dụng bắt đầu từ 10:44 25/07/2015<br/>Xin chân trọng cảm ơn!";
		}
		


        if ($this->email->send()) {
            
        }
    }

    public function campaignSendGiftCouponDb($mac, $nasid, $id_campaign, $name_device, $id_place, $mobile, $fullname, $email) {
        
        if ($this->min_length($mobile, 10) == 1) {
            return array('status' => '201', 'content' => 'Vui lòng nhập số điện thoại hợp lệ');
        }
        if ($this->min_length($mobile, 10) == 2) {
            return array('status' => '201', 'content' => 'Vui lòng nhập số điện thoại hợp lệ');
        }
        if ($this->valid_email($email) == 1) {
            return array('status' => '202', 'content' => 'Vui lòng nhập email hợp lệ');
        }

        $time_active = $this->db->query('SELECT time_active FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object()->time_active;
        if (in_array($time_active, array('', null))) {
            return array('status' => '203', 'content' => 'Không tồn tại time active');
        }
        
       
        $now_ = new MongoInt64(date("YmdHis", time()));
        $query = $this->m_db->where(array('id_campaign' => (int) $id_campaign, 'active' => 0))->where_gt('time_end_vs', $now_)->limit(1);
        $result = $query->get('log_campaign_coupon');
        if (count($result) == 0) {
            return array('status' => '204', 'content' => 'Hết coupon, bạn không thể nhận');
        } else {

            $oneview = $this->db->query('SELECT oneview FROM campaign WHERE id = ' . $id_campaign . ' LIMIT 1')->row_object()->oneview;
            if (in_array($oneview, array('', null))) {
                $oneview = 1;
            }
            // count interact
            $count_interact = $this->m_db->where(array('mobile' => (string) $mobile, 'id_campaign' => (int) $id_campaign))->count('log_campaign_coupon');
            if ($count_interact >= $oneview) {
                return array('status' => '205', 'content' => 'Bạn đã nhận quá số lượng cho phép');
            }

            $count_interact = $this->m_db->where(array('email' => (string) $email, 'id_campaign' => (int) $id_campaign))->count('log_campaign_coupon');
            if ($count_interact >= $oneview) {
                return array('status' => '205', 'content' => 'Bạn đã nhận quá số lượng cho phép');
            }


            //return 'count_interact:'.$count_interact;

            $data = $result;
            //die(''.$data[0]['code_coupon']);
            //echo '<pre>';die(print_r($data));
            ini_set('mongo.native_long', 1);
            $now = date("Y-m-d H:i:s", time());
            $nr = date('YmdHis');
            $cd__ = ($nr);
            $cd2__ = (date("YmdHis", time() + 3600 * $time_active));
            $time_start_use = date("Y-m-d H:i:s", time() + 3600 * $time_active);
            $arr = array(
                'name_device' => $name_device,
                'status_sms' => (int) 0,
                'id_place' => intval($id_place),
                'mac' => $mac,
                'nasid' => $nasid,
                'active' => (int) 1, // update đã nhận
                'time_get' => date('H:i:s d-m-Y'),
                'time_get_vs' => $cd__,
                'time_start_use' => $time_start_use,
                'time_start_use_vs' => $cd2__,
                'mobile' => (string)$mobile,
                'email' => (string)$email
            );
             
            $this->sendEmail($email, $data[0]['code_coupon'], $time_start_use, $id_place);

            $this->m_db->set($arr)->where(array('id_campaign' => (int) $id_campaign, 'code_coupon' => (string) $data[0]['code_coupon']))->update('log_campaign_coupon', array(), array());
            // VOID() + amount_get
            
            $data_amount = $this->db->query('SELECT amount, amount_get FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object();
           
            if ($data_amount->amount <= $data_amount->amount_get) {
               
                return array('status' => '206', 'content' => 'coupon phát ra nho hon coupon đã nhận');
            } else {
                
                $amount_get = (int) $data_amount->amount_get + 1;
                $this->db->update('campaign_coupon', array('amount_get' => $amount_get), 'id_campaign = ' . $id_campaign);
                //die('555');
                return array('status' => '200','content' => 'Thành công');
            }
        }
    }
	
	public function syncgetcouponDb($code_coupon, $email){
		if( $this->valid_email($email) == 1 ){
                return '201';
		}
		$now_ = new MongoInt64(date("YmdHis", time()));
        //$query = $this->m_db->where(array('code_coupon' => (string) $code_coupon, 'active' => 0))->where_gt('time_end_vs', $now_)->limit(1);
        $query = $this->m_db->where(array('code_coupon' => (string) $code_coupon, 'active' => 0))->limit(1);
        $result = $query->get('log_campaign_coupon');
		if (count($result) == 0) {
            return '202'; // không tồn tại code coupon này hoặc nó đã active.
        } else {
            $data = $result;
			$id_campaign = $data[0]['id_campaign'];
			$id_place = 0;// do nhiều địa điểm éo biết địa điểm nào :))
            //die(''.$data[0]['code_coupon']);
            //echo '<pre>';die(print_r($data));
            ini_set('mongo.native_long', 1);
            $now = date("Y-m-d H:i:s", time());
            $nr = date('YmdHis');
            $cd__ = ($nr);
            $cd2__ = (date("YmdHis", time() + 3600 * $time_active));
            $time_start_use = date("Y-m-d H:i:s", time() + 3600 * $time_active);
            $arr = array(
                'type' => 2, // 1 mobile, 2 email
                'name_device' => 'Đang cập nhật',
                'status_sms' => (int) 0,
                'id_place' => intval($id_place),
                'mac' => 'Đang cập nhật',
                'nasid' => 'Đang cập nhật',
                'active' => (int) 1, // update đã nhận
                'time_get' => date('H:i:s d-m-Y'),
                'time_get_vs' => $cd__,
                'time_start_use' => $time_start_use,
                'time_start_use_vs' => $cd2__
            );
                $arr['email'] = $email;
            $this->m_db->set($arr)->where(array('id_campaign' => (int) $id_campaign, 'code_coupon' => (string)$code_coupon ))->update('log_campaign_coupon', array(), array());
            // VOID() + amount_get 
            $data_amount = $this->db->query('SELECT amount, amount_get FROM campaign_coupon WHERE id_campaign = ' . $id_campaign)->row_object();
            if ($data_amount->amount <= $data_amount->amount_get) {
                return '203'; // coupon phát ra <= coupon đã nhận     
            } else {
                $amount_get = (int) $data_amount->amount_get + 1;
                $this->db->update('campaign_coupon', array('amount_get' => $amount_get), 'id_campaign = ' . $id_campaign);
                return '200';
            }
		}
	}
	
}
